//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls 
{
    public class SendEmail : TemplatedWebControl
    {
        #region Child Controls

        TextBox subject;
        Editor message;
        Literal FromUser;
        Literal ToUser;
        RequiredFieldValidator validator;
        IButton PostButton;
        IButton CloseButton;

        CSContext csContext = CSContext.Current;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "View-SendEmail.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            // Who the email is from
            //
            FromUser.Text = csContext.User.DisplayName + " (" + csContext.User.Email + ")";

            // Who the email is to
            //
            User toUser = Users.GetUser(csContext.UserID, false);
            if (toUser != null && toUser.IsRegistered) 
                ToUser.Text = toUser.DisplayName;
            else 
                throw new CSException(CSExceptionType.UserNotFound);

            Users.ValidateUserAccess(toUser, csContext.User);

            if (message != null)
                message.EnableHtmlModeEditing = true;
            
            if (validator != null)
                validator.ErrorMessage = ResourceManager.GetString("SendEmail_Validator");

            if (PostButton != null)
                PostButton.Text = ResourceManager.GetString("SendEmail_Send");

            if (CloseButton != null)
            {
                CloseButton.CausesValidation = false;
                CloseButton.Text = ResourceManager.GetString("Close_Window");
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            FromUser = (Literal) FindControl("FromUser");     
            ToUser = (Literal) FindControl("ToUser");
            subject = (TextBox) FindControl("Subject");
            message = FindControl("Message") as Editor;
            validator = (RequiredFieldValidator) FindControl("SubjectValidator");
            PostButton = FindButton("PostButton");
            CloseButton =  FindButton("CloseButton");
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
		    if (PostButton != null)
                PostButton.Click += new EventHandler(SendEmail_Click);

            if (CloseButton != null)
                CloseButton.Click += new EventHandler(CancelEmail_Click);
        }
		        
        #endregion

        #region Event Handlers

        void SendEmail_Click(Object sender, EventArgs e) 
        {			
            // Use a post for email formatting
            //
            MailPost post = new MailPost();
            post.Subject = subject.Text;
            post.Body = message.Text;
            post.PostType = message.IsRichTextCapable ? PostContentType.HTML : PostContentType.Text;
            post.FormattedBody = Transforms.FormatPostText(message.Text, post.PostType);			
            post.PostDate = DateTime.Now;
            post.User = csContext.User;
            post.Username = csContext.User.Username;

            User toUser = Users.GetUser( csContext.UserID, true );
            if (toUser != null)
                Emails.UserToUser(csContext.User, toUser, post);

            // Context.Response.Redirect(Globals.GetSiteUrls().Message(CSExceptionType.EmailSentToUser));
            
            Modal.ClosePage(this.Page);
        }

        void CancelEmail_Click(Object sender, EventArgs e) 
        {
            Modal.ClosePage(this.Page);

            /*
            string redirectUrl = string.Empty;

            if (!csContext.User.IsAnonymous)
                redirectUrl = Globals.GetSiteUrls().UserProfile(csContext.UserID);
            else
                redirectUrl = Globals.GetSiteUrls().ForumsHome;

            Page.Response.Redirect(redirectUrl);
            Page.Response.End();
            */
        }

        #endregion
    }
}